<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>模拟Java中的map类</title>
</head>
<body>
<script type="text/javascript">
	
	function Map() {
		var obj = new Object();

		this.put = function (key, value) {
			obj[key] = value;
		}

		this.size = function () {
			var count = 0;
			for(var item in obj){
				count++;
			}
			return count;
		}

		this.get = function (key) {
			if(obj[key] || obj[key] === 0 || obj[key] === false){
				return obj[key];
			} else {
				return null;
			}
		}

		this.set = function (key, value) {
			if(obj[key] || obj[key] === 0 || obj[key] === false){
				obj[key] = value;
			} else {
				throw new Error('key does not exist');
			}
		}

		this.remove = function (key) {
			if(obj[key] || obj[key] === 0 || obj[key] === false){
				delete obj[key]
			} else {
				throw new Error('key does not exist');
			}
		}

		this.eachMap = function (fn) {
			for(var key in obj){
				fn(key, obj[key]);
			}
		}

		this.clear = function () {
			obj = {};
		}

		this.toString = function () {
			return JSON.stringify(obj);
		}

		this.isEmpty = function () {
			return obj == {};
		}

	}

	var m = new Map();
	m.put('01',1);
	m.put('02',2);
	m.put('03',5);
	m.put('04',4);
	//m.remove('1');
	//console.log(m.get('1'));
	/*m.eachMap(function (key, value) {
		console.log(key, value);
	})*/
	console.log(m.isEmpty());

</script>
</body>
</html>